#define _CRT_SECURE_NO_WARNINGS

#include "All.h"

void PrintArray(int* a, int n)
{
	assert(a);

	for (int i = 0; i < n; i++) printf("%d ", a[i]);
	printf("\n");
}

void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void InsertSort(int* a, int n)
{
	assert(a);

	for (int i = 1; i < n; i++)
	{
		int end = i - 1;
		int tmp = a[i];
		while (end >= 0)
		{
			if (a[end] > tmp)
			{
				a[end + 1] = a[end];
				end--;
			}
			else break;
		}
		a[end + 1] = tmp;
	}
}

void SherSort(int* a, int n)
{
	assert(a);

	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (a[end] > tmp)
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else break;
			}
			a[end + gap] = tmp;
		}
	}
}

void SelectSort(int* a, int n)
{
	assert(a);

	int left = 0, right = n - 1;
	while (left < right)
	{
		int mini = left, maxi = left;
		for (int i = left; i <= right; i++)
		{
			if (a[i] < a[mini]) mini = i;
			if (a[i] > a[maxi]) maxi = i;
		}
		swap(&a[left], & a[mini]);
		if (maxi == left) maxi = mini;
		swap(&a[right], &a[maxi]);

		left++;
		right--;
	}
}

void adjustdown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child]) child++;
		if (a[child] > a[parent])
		{
			swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else break;
	}
}
void HeapSort(int* a, int n)
{
	assert(a);

	for (int i = (n - 2) / 2; i >= 0; i--) adjustdown(a, n, i);

	int end = n - 1;
	while (end > 0)
	{
		swap(&a[end], &a[0]);
		adjustdown(a, end, 0);
		end--;
	}
}

void BubbleSort(int* a, int n)
{
	assert(a);

	for (int i = 0; i < n - 1; i++)
	{
		bool flag = true;
		for (int j = 1; j < n - i; j++)
		{
			if (a[j - 1] > a[j])
			{
				swap(&a[j - 1], &a[j]);
				flag = false;
			}
		}
		if (flag) break;
	}
}

int mid_num(int* a, int l, int r)
{
	int mid = (l + r) / 2;
	if (a[l] > a[r])
	{
		if (a[r] > a[mid]) return r;
		else if (a[l] < a[mid]) return l;
		else return mid;
	}
	else
	{
		if (a[l] > a[mid]) return l;
		else if (a[r] < a[mid]) return r;
		else return mid;
	}
}
void QuickSort(int* a, int l, int r)
{
	if (l >= r) return;

	int mid = QuickSort1(a, l, r);

	QuickSort(a, l, mid - 1);
	QuickSort(a, mid + 1, r);
}
//void QuickSort1(int* a, int l, int r)
//{
//	if (l >= r) return;
//
//	int mid = mid_num(a, l, r);
//	swap(&a[mid], &a[l]);
//	int keyi = l, begin = l, end = r;
//
//	while (begin < end)
//	{
//		while (begin < end && a[end] >= a[keyi]) end--;
//		while (begin < end && a[begin] <= a[keyi]) begin++;
//
//		swap(&a[begin], &a[end]);
//	}
//	swap(&a[keyi], &a[begin]);
//
//	QuickSort1(a, l, begin - 1);
//	QuickSort1(a, begin + 1, r);
//}
//void QuickSort2(int* a, int l, int r)
//{
//	if (l >= r) return;
//
//	int mid = mid_num(a, l, r);
//	swap(&a[mid], &a[l]);
//	int hole = l, key = a[l];
//	int begin = l, end = r;
//
//	while (begin < end)
//	{
//		while (begin < end && a[end] >= key) end--;
//		a[hole] = a[end];
//		hole = end;
//
//		while (begin < end && a[begin] <= key) begin++;
//		a[hole] = a[begin];
//		hole = begin;
//	}
//	a[hole] = key;
//
//	QuickSort2(a, l, hole - 1);
//	QuickSort2(a, hole + 1, r);
//}
//void QuickSort3(int* a, int l, int r)
//{
//	if (l >= r) return;
//
//	int mid = mid_num(a, l, r);
//	swap(&a[mid], &a[l]);
//	int prev = l, cur = l + 1, keyi = l;
//
//	while (cur <= r)
//	{
//		if (a[cur] < a[keyi] && ++prev != cur)
//			swap(&a[prev], &a[cur]);
//
//		cur++;
//	}
//	swap(&a[keyi], &a[prev]);
//
//	QuickSort3(a, l, prev - 1);
//	QuickSort3(a, prev + 1, r);
//}
int QuickSort1(int* a, int l, int r)
{
	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int keyi = l, begin = l, end = r;

	while (begin < end)
	{
		while (begin < end && a[end] >= a[keyi]) end--;
		while (begin < end && a[begin] <= a[keyi]) begin++;

		swap(&a[begin], &a[end]);
	}
	swap(&a[keyi], &a[begin]);

	return begin;
}
int QuickSort2(int* a, int l, int r)
{
	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int hole = l, key = a[l];
	int begin = l, end = r;

	while (begin < end)
	{
		while (begin < end && a[end] >= key) end--;
		a[hole] = a[end];
		hole = end;

		while (begin < end && a[begin] <= key) begin++;
		a[hole] = a[begin];
		hole = begin;
	}
	a[hole] = key;

	return hole;
}
int QuickSort3(int* a, int l, int r)
{
	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int prev = l, cur = l + 1, keyi = l;

	while (cur <= r)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
			swap(&a[prev], &a[cur]);

		cur++;
	}
	swap(&a[keyi], &a[prev]);

	return prev;
}

int tmp[1000];
void MergeSort(int* a, int l, int r)
{
	if (l >= r) return;

	int mid = (l + r) / 2;

	MergeSort(a, l, mid), MergeSort(a, mid + 1, r);

	int k = 0, i = l, j = mid + 1;
	while (i <= mid && j <= r)
	{
		if (a[i] < a[j]) tmp[k++] = a[i++];
		else tmp[k++] = a[j++];
	}
	while (i <= mid) tmp[k++] = a[i++];
	while (j <= r) tmp[k++] = a[j++];

	for (i = l, j = 0; i <= r; i++, j++) a[i] = tmp[j];
}